﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimhoppKjellsFavoriterVT13
{
    public partial class Main : Form, IMain
    {
        #region Variables
        private CountryList Countries = new CountryList();

        private List<Judge> currentJumpsJudges = new List<Judge>();



        #endregion
        public Main()
        {
            InitializeComponent();
            this.inputJudgeNationalityCombobox.DataSource = Countries.countryList;
            this.inputContestantNationalityCombobox.DataSource = Countries.countryList;

        }

        public bool NewContest()
        {

            //int d = (ContestInformation.date.CompareTo(ContestInformation.deadLine));
            string d = ContestInformation.date.ToLongTimeString(); //- ContestInformation.deadLine.ToUniversalTime();
            TestKonsol.Konsoltestsklass.stringToConsole(d);
            if (ContestInformation.date.CompareTo(ContestInformation.deadLine) > 0)
            {
                Contest c = new Contest(ContestInformation.name, ContestInformation.date, ContestInformation.deadLine);
                currentPresenter = new PresenterMain(this, c);
                addRandomPeople();
                return true;
                
            }

            else
            {

                MessageBox.Show("Deadline can not be past the contest date!");
                return false;

            }

        }

        public bool save()
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "XML Files|*.xml";
            saveFile.Title = "Save an XML File";
            saveFile.ShowDialog();

            if (saveFile.FileName != "")
            {
                this.EventSaveToFile(saveFile.FileName);
                return true;
            }

            else
                return false;
        }

        public bool load()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "XML Files|*.xml";
            openFile.Title = "Select an XML File";
            openFile.ShowDialog();

            if (openFile.FileName != "")
            {
                Contest c = new Contest(ContestInformation.name, ContestInformation.date, ContestInformation.deadLine);
                currentPresenter = new PresenterMain(this, c);
                this.EventReadFromFile(openFile.FileName);
                contestInfoPanel.Visible = true;
                contestInputPeoplePanel.Visible = true;
                UpdateJudgeListBox();
                UpdateContestantListBox();
                return true;
            }
            else
                return false;
        }


        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            contestInfoPanel.Visible = true;
        }

        private void panel1Prev_Click(object sender, EventArgs e)
        {
            contestInfoPanel.Visible = false;
            contestInputPeoplePanel.Visible = false;
        }

        private void panel1Next_Click(object sender, EventArgs e)
        {

            ContestInformation.deadLine = deadlineField.Value;
            ContestInformation.date = startDateField.Value;
            if (NewContest())
            {
                contestInputPeoplePanel.Visible = true;
                currentContestNameBox.Text = currentPresenter.contest.name;
            }
            
            //currentContestNameBox.Refresh();

        }


        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void newContestantBtn_Click(object sender, EventArgs e)
        {
            inputContestantInfoPanel.Visible = true;
        }

        private void newJudgeBtn_Click(object sender, EventArgs e)
        {
            inputContestantInfoPanel.Visible = true;
            inputJudgeInfoPanel.Visible = true;


        }
        #region judgeinfo-funktioner

        private void judgeInfoPanelOkBtn_Click(object sender, EventArgs e)
        {
            if (inputJudgeNameBox.Text != "" && inputJudgeNationalityCombobox.Text != "Select Country...")
            {
                AddJudgeData();
                editing = false;
                ClearJudgeData();
                inputJudgeInfoPanel.Visible = false;
                inputContestantInfoPanel.Visible = false;

                UpdateJudgeListBox();
            }
            else
            {
                MessageBox.Show("Insert a name and/or country", "Error", MessageBoxButtons.OK);
            }
        }

        private void judgeInfoPanelCancelBtn_Click(object sender, EventArgs e)
        {
            ClearJudgeData();
            editing = false;
            inputJudgeInfoPanel.Visible = false;
            inputContestantInfoPanel.Visible = false;

            UpdateContestantListBox();
        }

        private void judgeInfoPanelNextBtn_Click(object sender, EventArgs e)
        {

            if (inputJudgeNameBox.Text != "" && inputJudgeNationalityCombobox.Text != "Select Country...")
            {
                AddJudgeData();
                editing = false;
                ClearJudgeData();
            }
            else
            {
                MessageBox.Show("Insert a name and/or country", "Error", MessageBoxButtons.OK);
            }

        }


        private void AddJudgeData()
        {
            if (judgeGenderFemaleBtn.Checked)
            {
                if (editing)
                    EventEditJudge(inputJudgeNameBox.Text, inputJudgeBirthdateBox.Value.Date, Gender.female, inputJudgeNationalityCombobox.Text, ((Judge)judgesListBox.SelectedItem).ID);
                else
                    EventNewJudge(inputJudgeNameBox.Text, inputJudgeBirthdateBox.Value.Date, Gender.female, inputJudgeNationalityCombobox.Text, currentPresenter.contest.nextID);
            }
            else if (judgeGenderMaleBtn.Checked)
            {
                if (editing)
                    EventEditJudge(inputJudgeNameBox.Text, inputJudgeBirthdateBox.Value.Date, Gender.male, inputJudgeNationalityCombobox.Text, ((Judge)judgesListBox.SelectedItem).ID);
                else
                    EventNewJudge(inputJudgeNameBox.Text, inputJudgeBirthdateBox.Value.Date, Gender.male, inputJudgeNationalityCombobox.Text, currentPresenter.contest.nextID);
            }
            else
            {
                if (editing)
                    EventEditJudge(inputJudgeNameBox.Text, inputJudgeBirthdateBox.Value.Date, Gender.unspecified, inputJudgeNationalityCombobox.Text, ((Judge)judgesListBox.SelectedItem).ID);
                else
                    EventNewJudge(inputJudgeNameBox.Text, inputJudgeBirthdateBox.Value.Date, Gender.unspecified, inputJudgeNationalityCombobox.Text, currentPresenter.contest.nextID);
            }


        }

        private void ClearJudgeData()
        {
            inputJudgeNameBox.ResetText();
            inputJudgeBirthdateBox.ResetText();
            //femaleBtn.ResetText();
            //maleBtn.ResetText();
            //unspecifiedBtn.ResetText();
        }

        #endregion



        #region contestantinfo-funktioner

        private void UpdateContestantListBox()
        {
            this.contestantsListBox.DataSource = null;
            this.contestantsListBox.DataSource = currentPresenter.contest.Contestants;
            this.contestantsListBox.DisplayMember = "name";
        }
        private void UpdateJudgeListBox()
        {
            this.judgesListBox.DataSource = null;
            this.judgesListBox.DataSource = currentPresenter.contest.Judges;
            this.judgesListBox.DisplayMember = "name";
        }

        private void contestantOkBtn_Click(object sender, EventArgs e)
        {
            if (this.inputContestantNameBox.Text != "" && this.inputContestantNationalityCombobox.Text != "Select Country...")
            {
                AddContestantData();
                editing = false;
                ClearData();
                currentPresenter.contest.sortedContestants();
                UpdateContestantListBox();

                inputContestantInfoPanel.Visible = false;
            }
            else
            {
                MessageBox.Show("Insert a name and/or country", "Error", MessageBoxButtons.OK);
            }
        }

        private void contestantCancelBtn_Click(object sender, EventArgs e)
        {
            ClearData();
            editing = false;
            currentPresenter.contest.sortedContestants();
            UpdateContestantListBox();

            inputContestantInfoPanel.Visible = false;
        }

        private void contestantNextBtn_Click(object sender, EventArgs e)
        {
            if (this.inputContestantNameBox.Text != "" && this.inputContestantNationalityCombobox.Text != "Select Country...")
            {
                AddContestantData();
                editing = false;
                ClearData();
                UpdateContestantListBox();
            }
            else
            {
                MessageBox.Show("Insert a name and/or country", "Error", MessageBoxButtons.OK);
            }
        }

        private void AddContestantData()
        {

            if (femaleBtn.Checked)
            {
                if (editing)
                    EventEditContestant(inputContestantNameBox.Text, inputContestantBirthdateBox.Value.Date, Gender.female, inputContestantNationalityCombobox.Text, ((Contestant)contestantsListBox.SelectedItem).ID);
                else
                    EventNewContestant(inputContestantNameBox.Text, inputContestantBirthdateBox.Value.Date, Gender.female, inputContestantNationalityCombobox.Text, currentPresenter.contest.nextID);
            }
            else if (maleBtn.Checked)
            {
                if (editing)
                    EventEditContestant(inputContestantNameBox.Text, inputContestantBirthdateBox.Value.Date, Gender.male, inputContestantNationalityCombobox.Text, ((Contestant)contestantsListBox.SelectedItem).ID);
                else
                    EventNewContestant(inputContestantNameBox.Text, inputContestantBirthdateBox.Value.Date, Gender.male, inputContestantNationalityCombobox.Text, currentPresenter.contest.nextID);
            }
            else
            {
                if (editing)
                    EventEditContestant(inputContestantNameBox.Text, inputContestantBirthdateBox.Value.Date, Gender.unspecified, inputContestantNationalityCombobox.Text, ((Contestant)contestantsListBox.SelectedItem).ID);
                else
                    EventNewContestant(inputContestantNameBox.Text, inputContestantBirthdateBox.Value.Date, Gender.unspecified, inputContestantNationalityCombobox.Text, currentPresenter.contest.nextID);
            }



        }
        private void ClearData()
        {
            inputContestantNameBox.ResetText();
            //inputNationalityBox.ResetText();
            inputContestantBirthdateBox.ResetText();
            //femaleBtn.ResetText();
            //maleBtn.ResetText();
            //unspecifiedBtn.ResetText();
        }

        #endregion


        private void showOrHideJumps(bool contestantselected)
        {
            if (contestantselected)
            {
                contestantJumpslistBox.Visible = true;
                contestantJumplistText.Visible = true;
                contestantlistAddJumpBtn.Visible = true;
                contestantlistDeleteJumpBtn.Visible = true;
                contestantlistShowJumpBtn.Visible = true;
            }
            else
            {
                contestantJumpslistBox.Visible = false;
                contestantJumplistText.Visible = false;
                contestantlistAddJumpBtn.Visible = false;
                contestantlistDeleteJumpBtn.Visible = false;
                contestantlistShowJumpBtn.Visible = false;
            }
        }


        private void contestantsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            showOrHideJumps(true);
            if (((Contestant)contestantsListBox.SelectedItem) != null)
            {
                //selectedContestantName.Text = ((Contestant)contestantsListBox.SelectedItem).name;
                //selectedContestantName.Refresh();

                selectedContestantNameBox.Text = ((Contestant)contestantsListBox.SelectedItem).name;
                selectedContestantNationalityBox.Text = ((Contestant)contestantsListBox.SelectedItem).nationality;
                selectedContestantBirthdateBox.Text = ((Contestant)contestantsListBox.SelectedItem).birthDate.ToShortDateString();
                selectedContestantTotalPointsBox.Text = ((Contestant)contestantsListBox.SelectedItem).getTotalScore().ToString();

                updateJumpListbox();
                bContestantSelected = true;

            }
            selectedContestantTotalPointsBox.Visible = true;
            selectedContestantTotalPointsLabel.Visible = true;
        }
        private void judgesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            showOrHideJumps(false);

            if (((Judge)judgesListBox.SelectedItem) != null)
            {
                //selectedContestantName.Text = ((Contestant)contestantsListBox.SelectedItem).name;
                //selectedContestantName.Refresh();

                selectedContestantNameBox.Text = ((Judge)judgesListBox.SelectedItem).name;
                selectedContestantNationalityBox.Text = ((Judge)judgesListBox.SelectedItem).nationality;
                selectedContestantBirthdateBox.Text = ((Judge)judgesListBox.SelectedItem).birthDate.ToShortDateString();

                bContestantSelected = false;

            }
            selectedContestantTotalPointsBox.Visible = false;
            selectedContestantTotalPointsLabel.Visible = false;
        }


        private void contestNameField_TextChanged(object sender, EventArgs e)
        {
            ContestInformation.name = contestNameField.Text;
        }

        private void deadlineField_ValueChanged(object sender, EventArgs e)
        {
            ContestInformation.deadLine = deadlineField.Value;
        }

        private void startDateField_ValueChanged(object sender, EventArgs e)
        {
            ContestInformation.date = startDateField.Value;
        }

        private void deleteJudgeBtn_Click(object sender, EventArgs e)
        {
            DialogResult b = MessageBox.Show("Are you sure?", "Warning!", MessageBoxButtons.YesNo);
            if (b == DialogResult.Yes)
            {
                if (judgesListBox.SelectedItem != null)
                {
                    EventDeleteJudge(((Person)judgesListBox.SelectedItem).ID);
                    UpdateJudgeListBox();
                }
                if (contestantsListBox.SelectedItem == null && judgesListBox.SelectedItem == null)
                {
                    selectedContestantNameBox.Text = "";
                    selectedContestantNationalityBox.Text = "";
                    selectedContestantBirthdateBox.Text = "";
                    selectedContestantTotalPointsBox.Text = "";
                }
            }
        }

        private void editPersonBtn_Click(object sender, EventArgs e)
        {
            if (bContestantSelected && contestantsListBox.SelectedItem != null)
            {
                editing = true;
                inputContestantInfoPanel.Visible = true;

                inputContestantBirthdateBox.Value = ((Contestant)contestantsListBox.SelectedItem).birthDate;
                inputContestantNameBox.Text = ((Contestant)contestantsListBox.SelectedItem).name;
                inputContestantNationalityCombobox.Text = ((Contestant)contestantsListBox.SelectedItem).nationality;
            }
            else if (judgesListBox.SelectedItem != null)
            {
                editing = true;

                inputContestantInfoPanel.Visible = true;
                inputJudgeInfoPanel.Visible = true;

                this.inputJudgeBirthdateBox.Value = ((Judge)judgesListBox.SelectedItem).birthDate;
                this.inputJudgeNameBox.Text = ((Judge)judgesListBox.SelectedItem).name;
                this.inputJudgeNationalityCombobox.Text = ((Judge)judgesListBox.SelectedItem).nationality;
            }



        }

        private void loadContestBtn_Click(object sender, EventArgs e)
        {
            load();
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void SetContest(Contest c)
        {
            throw new NotImplementedException();
        }



        private void contestantlistAddJumpBtn_Click(object sender, EventArgs e) //add jump
        {
            if ((Contestant)contestantsListBox.SelectedItem != null)
            {
                inputContestantInfoPanel.Visible = true;
                inputJudgeInfoPanel.Visible = true;
                addJumpPanel.Visible = true;

                currentJumpsJudges.Clear();
                addJumpAddedJudges.DataSource = null;
                addJumpAddedJudges.DataSource = currentJumpsJudges;
                addJumpAddedJudges.DisplayMember = "name";


                addJumpAddJudges.DataSource = currentPresenter.contest.Judges;
                addJumpAddJudges.DisplayMember = "name";

            }
        }

        private void inputJumpOk_Click(object sender, EventArgs e)      //inputJump
        {
            int i = 0;

            this.EventNewJump(((Contestant)contestantsListBox.SelectedItem).ID, (float)inputJumpDificultyBox.Value, inputJumpNameBox.Text, currentJumpsJudges);

            showOrHideJumps(true);
            inputContestantInfoPanel.Visible = false;
            inputJudgeInfoPanel.Visible = false;
            addJumpPanel.Visible = false;
            updateJumpListbox();
            
        }

        private void inputJumpCancel_Click(object sender, EventArgs e)
        {

            showOrHideJumps(true); 
            inputContestantInfoPanel.Visible = false;
            inputJudgeInfoPanel.Visible = false;
            addJumpPanel.Visible = false;
            updateJumpListbox();
            
        }

        private void inputJumpNext_Click(object sender, EventArgs e)
        {
            this.EventNewJump(((Contestant)contestantsListBox.SelectedItem).ID, (float)inputJumpDificultyBox.Value, inputJumpNameBox.Text, currentJumpsJudges);
            inputJumpNameBox.Text = "";
            currentJumpsJudges.Clear();
            inputJumpDificultyBox.Value = 0;
            updateJumpListbox();


            addJumpAddedJudges.DataSource = null;
            addJumpAddedJudges.DataSource = currentJumpsJudges;
            addJumpAddedJudges.DisplayMember = "name";


        }

        private void updateJumpListbox()
        {
            contestantJumpslistBox.DataSource = null;
            if (((Contestant)contestantsListBox.SelectedItem) != null)
            {
                if (((Contestant)contestantsListBox.SelectedItem).listOfJumps != null)
                {
                    this.contestantJumpslistBox.DataSource = ((Contestant)contestantsListBox.SelectedItem).listOfJumps;
                    this.contestantJumpslistBox.DisplayMember = "jumpID";
                }
            }
        }


        private void contestantJumpslistBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //updateJumpListbox();
        }



        public event DelegateNewContestant EventNewContestant;

        public event DelegateDeleteContestant EventDeleteContestant;

        public event DelegateNewJudge EventNewJudge;

        public event DelegateDeleteJudge EventDeleteJudge;

        public event DelegateSaveToFile EventSaveToFile;

        public event DelegateReadFromFile EventReadFromFile;
        public event DelegateNewContest EventNewContest;
        public event DelegateEditJudge EventEditJudge;
        public event DelegateEditContestant EventEditContestant;

        public event DelegateNewJump EventNewJump;
        public event DelegateEditJump EventEditJump;
        public event DelegateDeleteJump EventDeleteJump;
        public event DelegatePrintContestants EventPrintContestant;
        public event DelegatePrintJudges EventPrintJudges;
        public event DelegatePrintResult EventPrintResult;

        private void contestantlistShowJumpBtn_Click(object sender, EventArgs e)
        {
            if (contestantJumpslistBox.SelectedItem != null)
            {
                updateJumpInfoPanel();

                inputContestantInfoPanel.Visible = true;
                inputJudgeInfoPanel.Visible = true;
                addJumpPanel.Visible = true;
                jumpInfoPanel.Visible = true;
            }
        }

        private void updateJumpInfoPanel()
        {
            Jump currentJump = ((Jump)contestantJumpslistBox.SelectedItem);
            jumpInfoName.Text = ((Jump)contestantJumpslistBox.SelectedItem).jumpID;
            jumpInfoDifficulty.Text = "difficulty:" + ((Jump)contestantJumpslistBox.SelectedItem).difficulty.ToString();
            jumpInfoContestantName.Text = ((Contestant)contestantsListBox.SelectedItem).name;
            jumpInfoJumporder.Text = "jump number " + ((contestantJumpslistBox.SelectedIndex) + 1).ToString();

            Label[] judgesTotalScoreLabel = new Label[] { jumpInfoJudge1Total, jumpInfoJudge2Total, jumpInfoJudge3Total, jumpInfoJudge4Total, jumpInfoJudge5Total, jumpInfoJudge6Total, jumpInfoJudge7Total };
            Label[] judgesLabel = new Label[] { jumpInfoJudge1, jumpInfoJudge2, jumpInfoJudge3, jumpInfoJudge4, jumpInfoJudge5, jumpInfoJudge6, jumpInfoJudge7 };
            NumericUpDown[] judgeNumerics = new NumericUpDown[] { jumpInfoJudge1Approach, jumpInfoJudge1Entry, jumpInfoJudge1Flight, jumpInfoJudge1Takeoff,
            jumpInfoJudge2Approach, jumpInfoJudge2Entry, jumpInfoJudge2Flight, jumpInfoJudge2Takeoff,
            jumpInfoJudge3Approach, jumpInfoJudge3Entry, jumpInfoJudge3Flight, jumpInfoJudge3Takeoff,
            jumpInfoJudge4Approach, jumpInfoJudge4Entry, jumpInfoJudge4Flight, jumpInfoJudge4Takeoff,
            jumpInfoJudge5Approach, jumpInfoJudge5Entry, jumpInfoJudge5Flight, jumpInfoJudge5Takeoff,
            jumpInfoJudge6Approach, jumpInfoJudge6Entry, jumpInfoJudge6Flight, jumpInfoJudge6Takeoff,
            jumpInfoJudge7Approach, jumpInfoJudge7Entry, jumpInfoJudge7Flight, jumpInfoJudge7Takeoff};

            foreach (Label l in judgesLabel)
            {
                l.Visible = false;
            }
            foreach (NumericUpDown n in judgeNumerics)
            {
                n.Visible = false;
            }

            for (int i = 0; i < judgesTotalScoreLabel.Length; i++)
            {
                judgesTotalScoreLabel[i].Text = "";
            }


            for (int i = 0; i < currentJump.Judges.Count; i++)
            {
                if (currentJump.getJudge(i + 1) != null /*&& judgeNumerics[i * 4] != null*/)
                {
                    judgesLabel[i].Visible = true;
                    judgesLabel[i].Text = currentJump.getJudge(i + 1).name;

                    judgeNumerics[i * 4].Visible = true;
                    judgeNumerics[i * 4 + 1].Visible = true;
                    judgeNumerics[i * 4 + 2].Visible = true;
                    judgeNumerics[i * 4 + 3].Visible = true;

                    judgeNumerics[i * 4].Value = (decimal)(currentJump.getJudge(i + 1).Approach);
                    judgeNumerics[i * 4 + 1].Value = (decimal)(currentJump.getJudge(i + 1).Entry);
                    judgeNumerics[i * 4 + 2].Value = (decimal)(currentJump.getJudge(i + 1).Flight);
                    judgeNumerics[i * 4 + 3].Value = (decimal)(currentJump.getJudge(i + 1).Takeoff);



                    judgesTotalScoreLabel[i].Text = currentJump.calculatePointsForJudge(i + 1).ToString();
                }
            }
            jumpInfoTotalTotal.Text = currentJump.calculateTotalPoints().ToString();
            selectedContestantTotalPointsBox.Text = ((Contestant)contestantsListBox.SelectedItem).getTotalScore().ToString();

        }


        private void jumpInfoPanelOkBtn_Click(object sender, EventArgs e)
        {
            updateJudgesOfJump();

            inputContestantInfoPanel.Visible = false;
            inputJudgeInfoPanel.Visible = false;
            addJumpPanel.Visible = false;
            jumpInfoPanel.Visible = false;


        }
        private void jumpInfoPanelCancelBtn_Click(object sender, EventArgs e)
        {
            inputContestantInfoPanel.Visible = false;
            inputJudgeInfoPanel.Visible = false;
            addJumpPanel.Visible = false;
            jumpInfoPanel.Visible = false;
        }

        private void updateJudgesOfJump()
        {
            NumericUpDown[] judgeNumerics = new NumericUpDown[] { jumpInfoJudge1Approach, jumpInfoJudge1Entry, jumpInfoJudge1Flight, jumpInfoJudge1Takeoff,
            jumpInfoJudge2Approach, jumpInfoJudge2Entry, jumpInfoJudge2Flight, jumpInfoJudge2Takeoff,
            jumpInfoJudge3Approach, jumpInfoJudge3Entry, jumpInfoJudge3Flight, jumpInfoJudge3Takeoff,
            jumpInfoJudge4Approach, jumpInfoJudge4Entry, jumpInfoJudge4Flight, jumpInfoJudge4Takeoff,
            jumpInfoJudge5Approach, jumpInfoJudge5Entry, jumpInfoJudge5Flight, jumpInfoJudge5Takeoff,
            jumpInfoJudge6Approach, jumpInfoJudge6Entry, jumpInfoJudge6Flight, jumpInfoJudge6Takeoff,
            jumpInfoJudge7Approach, jumpInfoJudge7Entry, jumpInfoJudge7Flight, jumpInfoJudge7Takeoff};

            List<float> judgeList = new List<float>();
            for (int i = 0; i < 7; i++)
            {


                judgeList.Add(((float)((int)(((float)(judgeNumerics[i * 4].Value * 2)) + 0.5f))) / 2);
                judgeList.Add(((float)((int)(((float)(judgeNumerics[i * 4 + 1].Value * 2)) + 0.5f))) / 2);
                judgeList.Add(((float)((int)(((float)(judgeNumerics[i * 4 + 2].Value * 2)) + 0.5f))) / 2);
                judgeList.Add(((float)((int)(((float)(judgeNumerics[i * 4 + 3].Value * 2)) + 0.5f))) / 2);
                /*
                judgeList.Add((float)judgeNumerics[i * 4].Value);
                judgeList.Add(  (float)judgeNumerics[i * 4 + 1].Value );
                judgeList.Add((float)judgeNumerics[i * 4 + 2].Value);
                judgeList.Add((float)judgeNumerics[i * 4 + 3].Value);
                 */
            }

            this.EventEditJump(((Contestant)contestantsListBox.SelectedItem).ID, contestantJumpslistBox.SelectedIndex + 1, judgeList);
        }

        private void jumpInfoPanelPreviousBtn_Click(object sender, EventArgs e)
        {
            updateJudgesOfJump();

            if (contestantJumpslistBox.SelectedIndex != 0)
            {
                contestantJumpslistBox.SetSelected(contestantJumpslistBox.SelectedIndex - 1, true);
                updateJumpInfoPanel();
            }
            else
            {
                contestantJumpslistBox.SetSelected((((Contestant)contestantsListBox.SelectedItem).listOfJumps.Count) - 1, true);
                updateJumpInfoPanel();
            }
        }

        private void jumpInfoPanelNextBtn_Click(object sender, EventArgs e)
        {
            updateJudgesOfJump();

            if (contestantJumpslistBox.SelectedIndex + 1 < ((Contestant)contestantsListBox.SelectedItem).listOfJumps.Count)
            {
                contestantJumpslistBox.SetSelected(contestantJumpslistBox.SelectedIndex + 1, true);
                updateJumpInfoPanel();
            }
            else
            {
                contestantJumpslistBox.SetSelected(0, true);
                updateJumpInfoPanel();
            }
        }

        private void addRandomPeople()
        {
            EventNewContestant("Winston Churchill", new DateTime(1874, 11, 30), Gender.male, "United Kingdom", 99);
            EventNewContestant("Anna Lindh", new DateTime(1980, 01, 01), Gender.female, "Sweden", 98);
            EventNewJudge("Kjell", new DateTime(1952, 01, 22), Gender.male, "Sweden", 97);
            EventNewJudge("Sauron", new DateTime(1856, 01, 22), Gender.male, "Mordor", 96);
            EventNewJudge("Alfred Nobel", new DateTime(183, 10, 21), Gender.male, "Sweden", 95);
            EventNewJudge("Morrigan", new DateTime(1990, 01, 01), Gender.female, "Korchari Wilds", 94);
            EventNewContestant("Kalle Anka", new DateTime(1934, 06, 09), Gender.male, "Ankeborg", 93);
            EventNewContestant("Joakim von Anka", new DateTime(1876, 06, 09), Gender.male, "Ankeborg", 92);
            EventNewJudge("Psy", new DateTime(1977, 12, 31), Gender.male, "Gangnam", 91);
            EventNewContestant("Perrin Aybara", new DateTime(1978, 01, 14), Gender.male, "Two Rivers", 90);
            EventNewJudge("Herp", new DateTime(1880, 10, 10), Gender.male, "?", 89);
            EventNewJudge("Derp", new DateTime(1880, 10, 10), Gender.male, "?", 88);
            EventNewContestant("Helan", new DateTime(1898, 01, 14), Gender.male, "?", 87);
            EventNewContestant("Halvan", new DateTime(1899, 06, 12), Gender.male, "?", 86);
            EventNewJudge("Derpeliderp", new DateTime(1880, 10, 10), Gender.male, "?", 1337);



            UpdateContestantListBox();
            UpdateJudgeListBox();
        }





        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This is an awesome diving program made by Jimmy, Erik, Aron and Nandin (a.k.a. Kjells Favoriter) from Sim & Spel @ Örebro Universitet.\n\nThe purpose of this program is to keep track of the diving contest by adding contestants, judges, jumps, scores etc.\n\nCopyright 2013", "About");
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (currentPresenter != null)
                save();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            load();
        }

        private void printJudgesButton_Click(object sender, EventArgs e)
        {
            if (currentPresenter != null)
                EventPrintJudges();
        }

        private void printResultButton_Click(object sender, EventArgs e)
        {
            if (currentPresenter != null)
                EventPrintResult();
        }
        private void PrintContestantsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(currentPresenter != null)
                EventPrintContestant();
        }

        private bool checkDoubleJudges()
        {
            foreach (Judge j in currentJumpsJudges)
            {
                if (((Judge)addJumpAddJudges.SelectedItem).ID == j.ID)
                    return false;
            }
            return true;
        }

        private void addJumpAddJudgeBtn_Click(object sender, EventArgs e)
        {
            if (addJumpAddJudges.SelectedItem != null && currentJumpsJudges.Count < 7)
            {
                if (checkDoubleJudges())
                {
                    addJumpAddedJudges.DataSource = null;
                    Judge j = (Judge)((Judge)addJumpAddJudges.SelectedItem).Clone();
                    currentJumpsJudges.Add(j);
                    addJumpAddedJudges.DataSource = currentJumpsJudges;
                    addJumpAddedJudges.DisplayMember = "name";
                }
                else
                    MessageBox.Show("Judge is already grading this jump!", "Error");
            }
            else
            {
                MessageBox.Show("No judge selected or to many judges!", "Error");
            }
        }

        private void addJumpDeleteJudgeBtn_Click(object sender, EventArgs e)
        {
            if (addJumpAddedJudges.SelectedItem != null)
            {

                    currentJumpsJudges.RemoveAt(addJumpAddedJudges.SelectedIndex);
                    addJumpAddedJudges.DataSource = null;
                    addJumpAddedJudges.DataSource = currentJumpsJudges;
                    addJumpAddedJudges.DisplayMember = "name";
            }
        }

        private void editContestantBtn_Click(object sender, EventArgs e)
        {
            if (bContestantSelected && contestantsListBox.SelectedItem != null)
            {
                editing = true;
                inputContestantInfoPanel.Visible = true;

                inputContestantBirthdateBox.Value = ((Contestant)contestantsListBox.SelectedItem).birthDate;
                inputContestantNameBox.Text = ((Contestant)contestantsListBox.SelectedItem).name;
                inputContestantNationalityCombobox.Text = ((Contestant)contestantsListBox.SelectedItem).nationality;
            }
            else if (judgesListBox.SelectedItem != null)
            {
                editing = true;

                inputContestantInfoPanel.Visible = true;
                inputJudgeInfoPanel.Visible = true;

                this.inputJudgeBirthdateBox.Value = ((Judge)judgesListBox.SelectedItem).birthDate;
                this.inputJudgeNameBox.Text = ((Judge)judgesListBox.SelectedItem).name;
                this.inputJudgeNationalityCombobox.Text = ((Judge)judgesListBox.SelectedItem).nationality;
            }
        }

        private void deleteContestantBtn_Click(object sender, EventArgs e)
        {
            DialogResult b = MessageBox.Show("Are you sure?", "Warning!", MessageBoxButtons.YesNo);
            if (b == DialogResult.Yes)
            {
                if (contestantsListBox.SelectedItem != null)
                {
                    EventDeleteContestant(((Person)contestantsListBox.SelectedItem).ID);
                    UpdateContestantListBox();
                }
                if (contestantsListBox.SelectedItem == null && judgesListBox.SelectedItem == null)
                {
                    selectedContestantNameBox.Text = "";
                    selectedContestantNationalityBox.Text = "";
                    selectedContestantBirthdateBox.Text = "";
                    selectedContestantTotalPointsLabel.Text = "";
                }
            }
        }

        private void contestantlistDeleteJumpBtn_Click(object sender, EventArgs e)//fixa
        {
            if (contestantsListBox.SelectedItem != null)
            {

                EventDeleteJump(((Contestant)contestantsListBox.SelectedItem).ID, contestantsListBox.SelectedIndex + 1);
                updateJumpListbox();
            }

            else
            {
                MessageBox.Show("No jumps to delete!");
            }
        }

        private void inputContestantInfoPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        
    }
}